package datamanagement;

import healthsolution.wk.R;


import datamanagement.UserChoser;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.LinearLayout;

public class UserChoser extends Activity  implements Runnable
{
	private static final String TAG = "UserChoser Activity";
	
	
	public Thread currentThread = new Thread(this);
	public Handler threadHandler = new Handler();	
	public String strUserType = null;
	//private Handler handler = new Handler();
	
	public UserChoser()
	{
		
	}
		
	public void run() {
		// TODO Auto-generated method stub
		
		Log.i(TAG, "[run] Run");									//DEBUG
		try
		{
			//threadHandler.postDelayed(this,1000);			// PARA HACER QUE EL HANDLER SEA CONSTANTE
			//threadHandler.sendEmptyMessage(0);			//FD PARA USAR HANDLER CON MENSAJES
			
       	
			Thread.sleep((long) 1500);	
		}catch(Exception Ex)
		{
			Log.e(TAG, "[run] Error: " +Ex.getMessage());									//DEBUG
		}
		    
	}
	
	
	
    
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		SharedPreferences settings;
		settings = getSharedPreferences("HS",MODE_PRIVATE);   
		strUserType=settings.getString("UserKind",null);
		
		Log.i(TAG, "[onCreate] onCreate : USER TYPE: "+strUserType);		
		 super.onCreate(savedInstanceState);	
		 
		 boSetView();
		 
		 try
		 {
			 voStartListenerButtons();
			
		 }catch(Exception ex)
		 {
			 Log.e(TAG, "[onCreate] Exception Start Listener: "+ex.getMessage());			 
			 Log.e(TAG, "[onCreate] Exception Start Listener: "+ex.getCause());
			 Log.e(TAG, "[onCreate] Exception Start Listener: "+ex.getLocalizedMessage());
		 }
		 try
		 {
			 currentThread.start();
			 threadHandler.postDelayed(this,1000);
			 	 
		 }catch(Exception ex)
		 {
			 Log.e(TAG, "[onCreate] Exception Thread: "+ex.getMessage());			 
		 }		 
	}
		
	public void voStartListenerButtons()
	{
		
		try
		{
			LinearLayout loLinearEscritura = (LinearLayout)findViewById(R.id.linearLayoutButtons); 
        	ImageButton btLeerDatos = (ImageButton) findViewById(R.id.iconBtnLeerDatos);    		
        	ImageButton btEscribitDatos = (ImageButton) findViewById(R.id.iconBtnEscribir);
        	ImageButton btOrdenMedica = (ImageButton) findViewById(R.id.iconBtnOrdenMed);
    		
        	Log.i(TAG,"[voStartListenerButtons] Dentro de buttons listeners. ");
        	
    		btLeerDatos.setOnClickListener(new OnClickListener()
    	    {     
    			public void onClick(View v) 
    			{				
    				Log.i(TAG,"[voStartListenerButtons] Dentro OnClick LeerDatos");
    				
    			    
    				 Intent IntLD = null;
					    Log.i(TAG,"[voStartListenerButtons] Voy a empezar activity LeerDatos. ");
					    
					    try
					    {
					    	IntLD=new Intent(getApplicationContext(),LeerDatos.class);
					    }catch(Exception Ex)
					    {
					    	Log.e(TAG,"[voStartListenerButtons] Exception LeerDatos Intent: "+Ex);	
					    }
					    try
						{
					    	try
					    	{
					    		threadHandler.removeCallbacks(currentThread);	
					    		threadHandler.removeCallbacksAndMessages(UserChoser.class);		
					    		currentThread.interrupt();
						    		if(Thread.currentThread()==currentThread)
						    		{
						    			Log.e(TAG,"[voStartListenerButtons] CurrentThread");						    			
						    			currentThread.stop();
						    		}
							}catch(Exception ex)
							{
								Log.e(TAG,"[voStartListenerButtons] Thread Stop Exception "+ex);	
							}
					    	try
					    	{
					    		startActivity(IntLD);
							}catch(Exception ex)
							{
								Log.e(TAG,"[voStartListenerButtons] StartActivity Exception "+ex);	
							}
						}catch(Exception ex)
						{
							Log.e(TAG,"[voStartListenerButtons] Exception LeerDatos Start: "+ex);	
						}
    			}
    	    });
    		
    		try
			{    	
    			if(strUserType.compareTo("P")==0)
    			{
    				loLinearEscritura.removeView(btEscribitDatos);	 
    				
    			}else
    			{
    				btEscribitDatos.setOnClickListener(new OnClickListener()	        		
	        	    {     
	        			public void onClick(View v) 
	        			{				
	        				Log.i(TAG,"[voStartListenerButtons] Dentro OnClick Escribir Datos");        				
	        			    
	        				try
	        				{
		        				threadHandler.removeCallbacks(currentThread);					    						    	
						    	threadHandler.removeCallbacksAndMessages(UserChoser.class);
						    	currentThread.interrupt();
		        				if(Thread.currentThread()==currentThread)
					    		{
		        					currentThread.stop();	
					    		}		        				
	        				}catch(Exception ex)
	        				{
	        					Log.e(TAG,"[voStartListenerButtons] Thread ERROR EXCEPTION: "+ex);
	        				}
	        			    ///			
	        			}
	        	    });
    			}	    		
			}catch(Exception ex)
			{
				Log.e(TAG,"[voStartListenerButtons] EscribirDatos ERROR EXCEPTION: "+ex);
			}
    		
    		
    		btOrdenMedica.setOnClickListener(new OnClickListener()
    	    {     
    			public void onClick(View v) 
    			{				
    				Log.i(TAG,"[voStartListenerButtons] Dentro OnClick Orden Medica");
    				
    				try
    				{
        				threadHandler.removeCallbacks(currentThread);					    						    	
				    	threadHandler.removeCallbacksAndMessages(UserChoser.class);
				    	currentThread.interrupt();
        				if(Thread.currentThread()==currentThread)
			    		{
        					currentThread.stop();	
			    		}		        				
    				}catch(Exception ex)
    				{
    					Log.e(TAG,"[voStartListenerButtons] Thread ERROR EXCEPTION: "+ex);
    				}
    				///
    			}   			
    	    });

    		//////////////////////////////////////// MINIMIZAR ////////////////////////////////////
			ImageButton btMinimizes = (ImageButton) findViewById(R.id.iconminimize_ch);    	    
        	btMinimizes.setOnClickListener(new OnClickListener()
    	    {     
    			public void onClick(View v) 
    			{				
    				voMinimizarApp();    				
    			}
    	    });
        	
		}catch(Exception ex)
		{
			Log.e(TAG, "[voStartListenerButtons] Exception Start Listener Buttons: "+ex.getMessage());
			Log.e(TAG, "[voStartListenerButtons] Exception Start Listener Buttons: "+ex.getCause());
			Log.e(TAG, "[voStartListenerButtons] Exception Start Listener Buttons: "+ex.getStackTrace());
			Log.e(TAG, "[voStartListenerButtons] Exception Start Listener Buttons: "+ex);
		}
		
    		        	
	}
	
	///////////////////////////////////////////////////// Set View //////////////////////////////
	public boolean boSetView()
	{
		// SharedPreferences settings = getSharedPreferences("HS",MODE_PRIVATE);
     	Log.i(TAG, "[boSetView] Set ContentView");
		setContentView(R.layout.pugral);
		
		return true;
	}

	///////////////////////////////////////////onRestart///////////////////////////////////////////////////////////
	 @Override
	 protected void onRestart() {
	     super.onRestart();  // Always call the superclass method first
	     
	 	Intent myIntent=null;
    	try
       	{
    		//myIntent = new Intent(cont, HTService.class);
    		
    		 myIntent=new Intent(this.getApplicationContext(),UserChoser.class);
	    		
	       	}catch(Exception ex)
	         {
	        	 Log.e("PositionCYSActivity","[onCreate] ERROR I EX: "+ex);		        
	         }	    	
	       	try
	       	{
	       		startActivity(myIntent);
	         }catch(Exception ex)
	         {
	        	 Log.e("PositionCYSActivity","[onCreate] ERROR A EX: "+ex);
	         }
	     // Activity being restarted from stopped state    
	 }
	 
		
	
	
	///////////////////////////////////////////////////////////////////////////////////////////
	public IBinder onBind(Intent arg0)
	{
	    return null;
	}
	
	/////////////////////////////////////////////////////////////////////////////////////////////
	// PARA QUE NO PUEDA REGRESAR A LA PANTALLA ANTERIOR
	@Override
	public void onBackPressed() {
		Log.i(TAG, "[onBackPressed] onBackPressed");
	    voMinimizarApp();
	}
	public void voMinimizarApp()
	{
		try
		{
			Intent startMain = new Intent(Intent.ACTION_MAIN);
	    	startMain.addCategory(Intent.CATEGORY_HOME);
	    	startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
	    	startActivity(startMain);
	    	
		}catch(Exception ex)
		{
			Log.i(TAG, "[voMinimizarApp] Exception: "+ex);
		}
		
	}
	
}
